//
//  FLLSettingViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/17.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLSettingViewController.h"
#import "FLLMacro.h"
#import "FLLSettingTableViewCell.h"
#import "FLLClearCache.h"
#import "MBProgressHUD.h"

@interface FLLSettingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) FLLClearCache *clearCache;
@property (nonatomic, copy) NSString *totalCache;
@end

@implementation FLLSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
     // 计算缓存大小
    self.totalCache = [[FLLClearCache clearCache] CalculateCache];
    
    [self createTableView];
    self.navigationItem.title = @"设置";
    [self.navigationController.navigationBar setTitleTextAttributes:@{[UIFont systemFontOfSize:17 * kWidthScale] : NSFontAttributeName}];
    
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"FLLSettingTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"settingTableCell"];
    

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return 1;
    } else if (section == 1) {
    
        return 2;
    } if (section == 2) {
        return 3;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLLSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingTableCell"];
    cell.nameLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    cell.contentLabel.font = [UIFont systemFontOfSize:13 * kWidthScale];
    if (indexPath.section == 0) {
        cell.nameLabel.text = @"分享给好友";
    } else if (indexPath.section == 1) {
    
        if (indexPath.row == 0) {
            cell.nameLabel.text = @"关于我们";
            cell.contentLabel.text = @"V4.5";
        } if (indexPath.row == 1) {
            cell.nameLabel.text = @"给我们打分";
        }
    } if (indexPath.section == 2){
    
        if (indexPath.row == 0) {
            cell.nameLabel.text = @"意见反馈";
        } if (indexPath.row == 1) {
            cell.nameLabel.text = @"清理缓存";
            cell.contentLabel.text = self.totalCache;
            
        } if (indexPath.row == 2) {
            cell.nameLabel.text = @"社区规则";
        }
    }
    return cell;
    
}
// group类型tableView改变自带区头区尾视图方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor = FLLLightGrayColor;
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 10 * kHeightScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    // 不能设置成0 否则无效
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 44 * kHeightScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            // 创建警示框
            UIAlertController *clearAlert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat: @"确定要清除缓存%@吗?", _totalCache] preferredStyle:UIAlertControllerStyleAlert];
            
            // 给警示框添加按钮 参数1：标题 参数2：风格  block块点击后的操作放在里面
            [clearAlert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            
            [self presentViewController:clearAlert animated:YES completion:^{
                
            }];

            [clearAlert addAction:[UIAlertAction
                              actionWithTitle:@"确定"
                              style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
            {
                self.totalCache = [[FLLClearCache clearCache] ClearCacheAndCalculateNew];
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.label.text =@"清理完成";
                [self.tableView reloadData];
                [hud hideAnimated:YES afterDelay:0.5f];
                
            }]];
            
           
        }
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
